package paf.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

import paf.importerandtranslators.FileStrategy;
import paf.model.FileStrategyContainer;


public class FileImportUI extends JPanel implements ActionListener {

	private JPanel pnlContent, pnlFile;
	private JLabel lblFile;
	private JTextField txtFile;
	private JButton btnBrowse, btnSave, btnImport;
	private JComboBox cmbFileTypes;
	private JFileChooser pnlFileChooser;
	private ArrayList<FileStrategyContainer> arrFiles;
	private FileStrategy fileStrategy;
	private UiController uiController;
	
	protected FileImportUI(UiController uiController) {
    	super(new BorderLayout());
        pnlFileChooser = new JFileChooser();
        arrFiles = new ArrayList<FileStrategyContainer>();
        this.uiController = uiController;
        
        renderView();
    }
	
	protected FileImportUI(UiController uiController, FileStrategy fS) {
    	super(new BorderLayout());
        pnlFileChooser = new JFileChooser();
        arrFiles = new ArrayList<FileStrategyContainer>();
        fileStrategy = fS;
        this.uiController = uiController;
        
        renderView();
    }

    private void renderView() {
		pnlContent = new JPanel();
			pnlFile = new JPanel(new BorderLayout());
				lblFile = new JLabel("File:");
				pnlFile.add(lblFile, BorderLayout.NORTH);
				txtFile = new JTextField(20);
				pnlFile.add(txtFile, BorderLayout.WEST);
				btnBrowse = new JButton("Browse");
				btnBrowse.addActionListener(this);
				if(fileStrategy == null) {
					cmbFileTypes = new JComboBox();
					for(FileStrategy fS : uiController.getFileTypes()) {
						cmbFileTypes.addItem(fS);
					}
					pnlFile.add(cmbFileTypes, BorderLayout.CENTER);
				}
				pnlFile.add(btnBrowse, BorderLayout.EAST);
				
				btnSave = new JButton("Save");
				btnSave.addActionListener(this);
				pnlFile.add(btnSave, BorderLayout.SOUTH);
			pnlContent.add(pnlFile);
		add(pnlContent, BorderLayout.WEST);
		
		btnImport = new JButton("Import");
		btnImport.addActionListener(this);
		add(btnImport, BorderLayout.SOUTH);	
	}

	@Override
	public void actionPerformed(ActionEvent e) {
        // Browse Files clicked
		if(e.getSource() == btnBrowse) {
        	browseFiles();
        } else if(e.getSource() == btnSave) {
        	addFile();
        } else if(e.getSource() == btnImport) {
        	uiController.fileImportNext(arrFiles);
        	System.out.println("Next clicked");
        }
    }
	
	private void browseFiles() {
		pnlFileChooser.setFileFilter(new FileNameExtensionFilter(fileStrategy.getDescription(), fileStrategy.getExt()));
		
		int returnVal = pnlFileChooser.showOpenDialog(FileImportUI.this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = pnlFileChooser.getSelectedFile();
            
            txtFile.setText(file.getAbsolutePath());
            System.out.println("Opening: " + file.getName() + ".");
        } else {
            System.out.println("Open command cancelled by user.");
        }
	}
	
	private void addFile() {
		File f = new File(txtFile.getText());
		if(f.exists()) {
			if(fileStrategy == null) {
				arrFiles.add(new FileStrategyContainer(f, (FileStrategy) cmbFileTypes.getSelectedItem()));
			} else {
				arrFiles.add(new FileStrategyContainer(f, fileStrategy));
			}
			txtFile.setText("");
			System.out.println("File added to array");
		} else {
			System.out.println("File could not be found");
		}
	}
}
